package com.hjx.movie.core.system.controller.admin;

import com.hjx.movie.common.utils.Result;
import com.hjx.movie.common.utils.ResultType;
import com.hjx.movie.core.system.service.admin.ICinemaHallService;
import com.hjx.movie.core.system.service.admin.ICinemaMovieShowService;
import com.hjx.movie.core.system.service.admin.ICinemaService;
import com.hjx.movie.core.system.service.admin.IMovieService;
import com.hjx.movie.persistence.system.model.IvyCinema;
import com.hjx.movie.persistence.system.model.IvyCinemaHall;
import com.hjx.movie.persistence.system.model.IvyCinemaMovieShow;
import com.hjx.movie.persistence.system.model.IvyMovie;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/10 - 17:36
 */
@Slf4j
@RestController
@RequestMapping("/movieShow")
public class CinemaMovieShowController {
    @Resource
    ICinemaMovieShowService cinemaMovieShowService;
    @Resource
    ICinemaHallService cinemaHallService;
    @Resource
    ICinemaService cinemaService;
    @Resource
    IMovieService movieService;

    @GetMapping("/findAll")
    public Result findAll(){
        return Result.success(cinemaMovieShowService.findAll());
    }

//    @GetMapping("/findByCondition")
//    public Result findCinemaByAreaId(@RequestParam(name = "areaId")Long areaId,
//                                     @RequestParam(name = "cinemaNameLike",required = false)String cinemaNameLike,
//                                     @RequestParam(name = "cinemaStatus",required = false)Integer cinemaStatus){
//        return Result.success(cinemaMovieShowService.findCinemaByCondition(areaId, cinemaNameLike, cinemaStatus));
//    }

    @GetMapping("/findSelectMap")
    public Result findMovieShowSelectMap(){
        List<IvyCinema> cinemaList =  cinemaService.findAllCinema();
        List<IvyCinemaHall> allCinemaHall = cinemaHallService.findAllCinemaHall();
        List<IvyMovie> movieList = movieService.findAllMovie();
        Map<Long,List<IvyCinemaHall>> map = new HashMap<>();
        for (IvyCinema cinema:cinemaList){
            List<IvyCinemaHall> cinemaHallList = allCinemaHall.stream()
                    .filter(item->item.getCinemaId().equals(cinema.getCinemaId()))
                    .collect(Collectors.toList());
            map.put(cinema.getCinemaId(),cinemaHallList);
        }
        Map<String,Object> data = new HashMap<>();
        data.put("cinemaList",cinemaList);
        data.put("cinemaHallList",allCinemaHall);
        data.put("cinemaHallMap",map);
        data.put("movieList",movieList);

        return Result.success(data);
    }


    @PostMapping("/add")
    public Result addCinema(@RequestBody IvyCinemaMovieShow movieShow){
        log.debug("{}",movieShow);
        if (cinemaMovieShowService.addMovieShow(movieShow)){
            return Result.success("新增排片成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"新增排片失败");
    }
    @GetMapping("/delete")
    public Result deleteCinema(@RequestParam("movieShowId") Long movieShowId){
        if (cinemaMovieShowService.deleteMovieShow(movieShowId)){
            return Result.success("删除排片成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"删除排片失败");
    }

    @PostMapping("/update")
    public Result updateCinema(@RequestBody IvyCinemaMovieShow movieShow){
        if (cinemaMovieShowService.updateMovieShow(movieShow)){
            return Result.success("修改排片成功");
        }
        return Result.error(ResultType.USER_INPUT_ERROR,"修改排片失败");
    }
    @GetMapping("queryMovieShowLike")
    public Result queryUserLike(@RequestParam(value = "movieShowLike") String str){
        if (StringUtils.isNotBlank(str)){
            return Result.success(cinemaMovieShowService.queryMovieShowLike(str));
        }
        return Result.success(cinemaMovieShowService.findAll());
    }


}
